package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-03-23
 * Time: 20:41
 */

// 详情页更新作者
@WebServlet("/detailSheet")
public class DetailSheetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 在博客详情页中, 在加载页面的时候, 需要从服务器后去到博客作者的用户信息, 把这个信息更新到页面上

        // 约定前端: GET / contentType: application/json
        // 约定后端: HTTP/ 1.1 200 ok contentType:application/json

        ObjectMapper objectMapper = new ObjectMapper();
        resp.setContentType("application/json; charset = utf8");
        req.setCharacterEncoding("utf8");
        // 获取会话 判定当前用户是否登陆
        HttpSession session = req.getSession(false);
        // 当前会话中包含 name  username count userId
        User user = (User) session.getAttribute("user");
        if(user == null) {
            return;
        }
        // 不用判定 user 强制登陆会检测 此处必定已经登陆
        user.setPassword("");
        // 把获取的会话中的 user java对象转成 json格式传给前端
        String respJson = objectMapper.writeValueAsString(user);
        resp.getWriter().write(respJson);


        // 约定请求: GET/ detailSheet?blogId = ? // 带上 blogId 区分那篇博客文章
        // 约定响应: HTTP/ 1.1 200 OK ContentType: application/json userId:? username:? 屏蔽掉密码

//        ObjectMapper objectMapper = new ObjectMapper();
//        // 获取 请求中的 blogId
//        String blogId = req.getParameter("blogId");
//        // 向查询指定 blogId 中的博客
//        BlogDao blogDao = new BlogDao();
//        Blog blog = blogDao.selectById(Integer.parseInt(blogId));
//        // 判断 blog 是否存在
//        if(blog == null) {
//            // 表明没有对应文章, 数据库查询不到该博客
//            resp.setContentType("text/html; charset = utf8");
//            resp.getWriter().write("当前对应 blogId: " + blogId + " 文章不存在!");
//            return;
//        }
//            UserDao userDao = new UserDao();
//            User user = userDao.selectById(blog.getUserId());
//            if(user == null) {
//                // 没有对应用户
//                resp.setContentType("text/html; charset = utf8");
//                resp.getWriter().write("当前 blogId:"+ blogId + "对应的 user 不存在!");
//                return;
//            }
//                // 当前 blogId 对应文章查询到的 user存在则返回
//                // 屏蔽掉获取的用户信息的密码
//                user.setPassword("");
//                String respJson = objectMapper.writeValueAsString(user);
//                resp.setContentType("application/json; charset = utf8");
//                resp.getWriter().write(respJson);

        }
    }

    /*private void getUserDetailSheet(HttpServletRequest req, HttpServletResponse resp, int blogId) throws IOException {
        // 根据 blogId 从服务器获取是那篇博客,从而获取到 userId
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectById(blogId);
        ObjectMapper objectMapper = new ObjectMapper();
        if(blog == null) {
            resp.setStatus(404);
            resp.setContentType("text/html; charset = utf8");
            resp.getWriter().write("当前 blogId : " + blogId + "不存在");
            System.out.println(blogId + "不存在");
            return;
        }
        // 根据 userId 查询对应的 user 对象
        UserDao userDao = new UserDao();
        // blog 是一篇博客, 每篇博客都存在 Id 正文...等信息
        User user = userDao.selectById(blog.getUserId());
        if(user == null) {
            resp.setStatus(404);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前blogId: " + blogId + "不存在");
            System.out.println("根据 userId查询用户不存在!");
            return;
        }

        // 把 user 返回给浏览器更新
        // 屏蔽用户密码
        user.setPassword("");
        resp.setContentType("application/json; charset=utf8");
        String respJson = objectMapper.writeValueAsString(user);
        resp.getWriter().write(respJson);


    }*/

